package com.pet.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.server.domain.entity.Goods;
import com.pet.server.domain.entity.GoodsOrder;
import com.pet.server.domain.entity.PetOwner;
import com.pet.server.domain.entity.Shop;
import com.pet.server.domain.vo.GoodsOrderVO;
import com.pet.server.mapper.GoodsOrderMapper;
import com.pet.server.service.IGoodsOrderService;
import com.pet.server.service.IGoodsService;
import com.pet.server.service.IPetOwnerService;
import com.pet.server.service.IShopService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class GoodsOrderServiceImpl implements IGoodsOrderService {
  private final GoodsOrderMapper goodsOrderMapper;
  @Resource IGoodsService gs;
  @Resource IPetOwnerService ps;
  @Resource IShopService is;

  /**
   * 适用BeanUtils包转VO数据
   *
   * @param item 源数据
   * @param goods 子数据
   * @param petMaster 子数据
   * @param gVo 目标数据
   * @throws IllegalAccessException IllegalAccessException异常
   * @throws InvocationTargetException InvocationTargetException异常
   */
  private void ConvertDataToVO(
      GoodsOrder item, Goods goods, PetOwner petMaster, Shop shop, GoodsOrderVO gVo)
      throws IllegalAccessException, InvocationTargetException {

    // BeanUtils.copyProperties(gVo, item);
    //
    // BeanUtils.copyProperty(gVo, "goodsId", goods.getId());
    // BeanUtils.copyProperty(gVo, "goodsName", goods.getGoodsName());
    //
    // BeanUtils.copyProperty(gVo, "petMasterId", petMaster.getId());
    // BeanUtils.copyProperty(gVo, "name", petMaster.getName());
    // BeanUtils.copyProperty(gVo, "phone", petMaster.getPhone());
    // BeanUtils.copyProperty(gVo, "address", petMaster.getAddress());
    //
    // BeanUtils.copyProperty(gVo, "shopId", shop.getId());
  }

  @Override
  public boolean updateGoodsOrderSelective(GoodsOrderVO vo) {
    //    int id = vo.getPetOwnerId();
    //    String address = vo.getAddress();
    //    PetOwner petMaster = new PetOwner();
    //
    //    petMaster.setId(id);
    //    petMaster.setMasterAddress(address);
    //
    boolean flag = false;
    //
    //    // 更新收货人地址
    //    boolean b = ps.updatePetOwnerSelective(petMaster);

    //    if (b) {
    //      GoodsOrder order = new GoodsOrder();
    //
    //      order.setId(vo.getId());
    //      order.setOrderStatus(vo.getStatus());
    //
    //      // 更新订单状态
    //      int i = goodsOrderMapper.insertOrUpdate(order);
    //
    //      if (i > 0) {
    //        flag = true;
    //      }
    //    }

    return flag;
  }

  @Override
  public List<GoodsOrderVO> getAllGoodsOrders(int pageNo, int pageSize, int shopId) {
    QueryWrapper<GoodsOrder> qw = new QueryWrapper<>();

    qw.eq("fk_shop_id", shopId);

    Page<GoodsOrder> goodsOrderPage = goodsOrderMapper.selectPage(new Page<>(pageNo, pageSize), qw);
    List<GoodsOrderVO> voList = new ArrayList<>();
    List<GoodsOrder> list = goodsOrderPage.getRecords();

    return getGoodsOrderVOS(list, voList);
  }

  @Override
  public GoodsOrderVO getGoodsOrderSelectiveById(int shopId, int goodsOrderId) {
    QueryWrapper<GoodsOrder> qw = new QueryWrapper<>();

    qw.eq("fk_shop_id", shopId);

    List<GoodsOrder> list = goodsOrderMapper.selectList(qw);

    System.out.println("list = " + list);

    GoodsOrder order = null;

    for (GoodsOrder item : list) {
      if (item.getId() == goodsOrderId) {
        order = item;
      }
    }

    Goods goods = gs.getGoodsSelectiveById(order.getGoodsId());
    //    PetOwner petMaster = ps.getPetOwnerSelectiveById(order.getPetOwnerId());
    Shop shop = is.detail(order.getShopId());
    GoodsOrderVO gVo = new GoodsOrderVO();

    //    try {
    //      ConvertDataToVO(order, goods, petMaster, shop, gVo);
    //    } catch (IllegalAccessException | InvocationTargetException e) {
    //      e.printStackTrace();
    //    }

    return gVo;
  }

  /**
   * 将对接数据库的数据 ,转换为对接view层的vo数据
   *
   * @param orderList DO数据源
   * @param goodsOrderVOList VO数据集合
   * @return vo数据集合
   */
  private List<GoodsOrderVO> getGoodsOrderVOS(
      List<GoodsOrder> orderList, List<GoodsOrderVO> goodsOrderVOList) {
    for (GoodsOrder item : orderList) {
      Goods goods = gs.getGoodsSelectiveById(item.getGoodsId());
      //      PetOwner petMaster = ps.getPetOwnerSelectiveById(item.getPetOwnerId());
      Shop shop = is.detail(item.getShopId());
      GoodsOrderVO gVo = new GoodsOrderVO();

      //      try {
      //        ConvertDataToVO(item, goods, petMaster, shop, gVo);
      //        goodsOrderVOList.add(gVo);
      //      } catch (IllegalAccessException e) {
      //        e.printStackTrace();
      //      } catch (InvocationTargetException e) {
      //        e.printStackTrace();
      //      }
    }

    return goodsOrderVOList;
  }

  @Override
  public List<GoodsOrderVO> getGoodsOrdersByGoodsName(
      int pageNo, int pageSize, String goodsName, int shopId) {
    QueryWrapper<GoodsOrder> qw = new QueryWrapper<>();

    qw.eq("fk_shop_id", shopId);

    Page<GoodsOrder> goodsOrderPage = goodsOrderMapper.selectPage(new Page<>(pageNo, pageSize), qw);
    List<GoodsOrder> list = goodsOrderPage.getRecords();
    List<GoodsOrder> orderList = new ArrayList<>();

    for (GoodsOrder item : list) {
      Goods goods = gs.getGoodsSelectiveById(item.getGoodsId());

      if (goods.getGoodsName().equalsIgnoreCase(goodsName)) {
        orderList.add(item);
      }
    }

    List<GoodsOrderVO> goodsOrderVOList = new ArrayList<>();

    return getGoodsOrderVOS(orderList, goodsOrderVOList);
  }

  public List<GoodsOrderVO> getGoodsOrdersByPetOwner(
      int pageNo, int pageSize, int shopId, String petMasterName) {
    QueryWrapper qw = new QueryWrapper();

    qw.eq("shop_id", shopId);

    Page<GoodsOrder> orderPages = goodsOrderMapper.selectPage(new Page<>(pageNo, pageSize), qw);
    List<GoodsOrder> list = orderPages.getRecords();
    List<GoodsOrder> tempList = new ArrayList<>();

    for (GoodsOrder item : list) {
      //      PetOwner petMaster = ps.getPetOwnerSelectiveById(item.getPetOwnerId());
      //
      //      if (petMaster.getMasterName().equalsIgnoreCase(petMasterName)) {
      //        tempList.add(item);
      //      }
    }

    List<GoodsOrderVO> goodsOrderVOList = new ArrayList<>();

    return getGoodsOrderVOS(tempList, goodsOrderVOList);
  }

  @Override
  public List<GoodsOrderVO> getGoodsOrdersByStatus(
      int pageNo, int pageSize, int shopId, String status) {
    QueryWrapper<GoodsOrder> qw = new QueryWrapper<>();

    qw.eq("fk_shop_id", shopId);

    Page<GoodsOrder> orderPages = goodsOrderMapper.selectPage(new Page<>(pageNo, pageSize), qw);
    List<GoodsOrder> list = orderPages.getRecords();
    List<GoodsOrder> tempList = new ArrayList<>();

    for (GoodsOrder item : list) {
      if (item.getOrderStatus().equalsIgnoreCase(status)) {
        tempList.add(item);
      }
    }

    List<GoodsOrderVO> goodsOrderVOList = new ArrayList<>();

    return getGoodsOrderVOS(tempList, goodsOrderVOList);
  }

  @Override
  public List<GoodsOrderVO> getGoodsOrdersByPetMaster(
      int pageNo, int pageSize, int shopId, String petMasterName) {
    return null;
  }
}

// ~ Formatted by Jindent --- http://www.jindent.com
